#include "SceneManager.h"
#include "../Physics/ParticleComponent.h"
#include "../Utils/Timer.h"
#include <Windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

using namespace Pantsu;

SceneManager::SceneManager( char *i_scnmgr_id )
	:
m_pcSceneManagerId( i_scnmgr_id ),
m_pPhysicsComponentsUpdater( 0 )
{
	m_pPhysicsComponentsUpdater = new PhysicsComponentsUpdater();
	m_pRenderComponentsUpdater  = new ComponentsUpdater();
	Timer::get().Init();
}

SceneManager::~SceneManager() {
}

GameObject *SceneManager::CreateGameObject(const std::string &i_id) {

	GameObject *go = new GameObject();
	m_GameObjects.push_back( go );

	return go;
}

void SceneManager::Update() {

	Timer::get().Update();
	float duration = (float)Timer::get().m_uLastFrameDuration * 0.001f;
    if ( duration >= 0.0f ) {
		/* Update Physics */
		m_pPhysicsComponentsUpdater->UpdateComponents( duration );
	
	}

	/* Draw Alla SceneGraph */
	
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor( 0.15f, 0.15f, 0.15f, 1.0f );
	/* HARDCODED */
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluPerspective( 50.0f, 640.0f / 480.0f, 0.0f, 500.0f );
	gluLookAt( -10.0f, 10.0f, 30.0f, 10.0f, 10.0f, 0.0f, 0.0f, 1.0f, 0.0f );
	/* end HARDCODED */

	/* Draw X Axis Origin */
	
	glBegin( GL_LINES );
	glColor3f(  1.0f, 0.0f, 0.0f );
	glVertex3f( 0.0f, 0.0f, 0.0f );
	glVertex3f( 5.0f, 0.0f, 0.0f );

	glColor3f(  0.0f, 1.0f, 0.0f );
	glVertex3f( 0.0f, 0.0f, 0.0f );
	glVertex3f( 0.0f, 0.0f, 5.0f );

	glColor3f(  0.0f, 0.0f, 1.0f );
	glVertex3f( 0.0f, 0.0f, 0.0f );
	glVertex3f( 0.0f, 5.0f, 0.0f );
	glEnd();


	for ( m_Itr = m_GameObjects.begin(); m_Itr != m_GameObjects.end(); ++m_Itr ) {
		
		m_pRenderComponentsUpdater->UpdateComponents( duration );
	}
}